import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../base/view/BaseState.dart';
import '../../base/view/BaseStatefulWidget.dart';
import '../../viewmodel/MainPageViewModel.dart';
import '../AllOneCateGoryList.dart';

class MyHomePage extends BaseStatefulWidget {
  const MyHomePage({super.key});

  @override
  BaseState<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends BaseState<MyHomePage> {
  final _mainPageViewModel = MainPageViewModel();

  void _gg() {
    // var firstLevelCategory = FirstLevelCategory(name: "xcxcx");
    // DatabaseHelper().insert(FirstLevelCategory(name: "xcxcx"));
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    name();
  }

  Future<void> name() async {
    await _mainPageViewModel.getALLOneCategory();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // AppBar 的标题
        title: const Text(
          'My Flutter App',
          style: TextStyle(),
        ),
        centerTitle: true,
        leading: Builder(
          builder: (BuildContext context) {
            return IconButton(
              icon: const Icon(Icons.search),
              onPressed: () {
                // Scaffold.of(context).openDrawer();
              },
              tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
            );
          },
        ),
        // AppBar 的操作按钮
        actions: [
          IconButton(
            icon: const Icon(Icons.select_all_outlined),
            onPressed: () {
              // 在这里处理搜索按钮的点击事件
              Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => const AllOneCateGoryList()));
            },
          ),
          IconButton(
            icon: const Icon(Icons.settings),
            onPressed: () {
              // 在这里处理设置按钮的点击事件
              Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => const AllOneCateGoryList()));
            },
          ),
        ],
      ),
      body: GridView.builder(
        itemBuilder: (context, index) {
          var card = Card(
            color: Colors.white,
            margin: const EdgeInsetsDirectional.fromSTEB(15, 5, 5, 5),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(

                  padding: const EdgeInsetsDirectional.all(10),
                  // padding: const EdgeInsetsDirectional.only(top: 50,bottom: 50),
                  // margin: const EdgeInsetsDirectional.only(top: 30),

                  //设置了 decoration 就不能设置color，两者只能存在一个
                  decoration: BoxDecoration(
                      color: Colors.grey.shade300,
                      //设置背景颜色
                      image: const DecorationImage(
                          image: AssetImage('images/tianjia1.png')),
                      borderRadius: const BorderRadius.all(Radius.circular(6))),
                  // child: const Image(
                  //   image: AssetImage("images/tianjia1.png"),
                  //   width: 45.0,
                  //   height: 45.0,
                  // ),
                ),
                // Row(
                //   mainAxisAlignment: MainAxisAlignment.end,
                //   children: [
                //     Container(
                //       margin: const EdgeInsetsDirectional.only(
                //           top: 20, end: 20, bottom: 10),
                //       // color: Colors.green,
                //       child: Text(
                //           style: const TextStyle(fontSize: 20),
                //           "数量 ${_mainPageViewModel.firList[index].amount}"),
                //     ),
                //   ],
                // )
              ],
            ),
          );
          // return card;
          return Container(
            child: Column(
              children: [
                card,
                // Text(
                //     style: const TextStyle(fontSize: 25),
                //     "${_mainPageViewModel.firList[index].name}")
              ],
            ),
          );
        },
        itemCount: _mainPageViewModel.firList.length,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3, childAspectRatio: 0.96),
      ),
    );
  }
}
